/*
 * Copyright (C) 2015 David Gao <davidgao1001@gmail.com>
 *
 * This program is free software; you can redistribute  it and/or modify it
 * under  the terms of  the GNU General  Public License as published by the
 * Free Software Foundation;  either version 2 of the  License, or (at your
 * option) any later version.
 *
 */

#ifndef _DRIVERS_SERIAL_UART_ZYNQ7000_H
#define _DRIVERS_SERIAL_UART_ZYNQ7000_H

/*
 * ZYNQ7000 UART driver
 */

/*
 * Include config.h to obtain UART base address (through
 * asm/mach-$MACH/config.h)
 */
#include <config.h>

#ifdef KERNEL
#define UART_BASE	uart1_base
#else /* KERNEL */
#define UART_BASE	UART1_PHYSBASE
#endif /* KERNEL */

/* Register Offset */
#define UART_OFFSET_CR		0x00
#define UART_OFFSET_MR		0x04
#define UART_OFFSET_IER		0x08
#define UART_OFFSET_IDR		0x0C
#define UART_OFFSET_IMR		0x10
#define UART_OFFSET_ISR		0x14
#define UART_OFFSET_BAUDGEN	0x18
#define UART_OFFSET_RXTOUT	0x1C
#define UART_OFFSET_RXWM	0x20
#define UART_OFFSET_MODEMCR	0x24
#define UART_OFFSET_MODEMSR	0x28
#define UART_OFFSET_SR		0x2C
#define UART_OFFSET_FIFO	0x30
#define UART_OFFSET_BAUDDIV	0x34
#define UART_OFFSET_FLOWDEL	0x38
#define UART_OFFSET_TXWM	0x44

/* CR */
#define UART_CR_STOPBRK		0x0100
#define UART_CR_STARTBRK	0x0080
#define UART_CR_TORST		0x0040
#define UART_CR_EN_DIS_MASK	0x003C
#define UART_CR_TX_DIS		0x0020
#define UART_CR_TX_EN		0x0010
#define UART_CR_RX_DIS		0x0008
#define UART_CR_RX_EN		0x0004
#define UART_CR_TXRST		0x0002
#define UART_CR_RXRST		0x0001

/* MR */
#define UART_MR_CCLK			0x00000400
#define UART_MR_CHMODE_MASK		0x00000300
#define UART_MR_CHMODE_R_LOOP		0x00000300
#define UART_MR_CHMODE_L_LOOP		0x00000200
#define UART_MR_CHMODE_ECHO		0x00000100
#define UART_MR_CHMODE_NORM		0x00000000
#define UART_MR_STOPMODE_MASK		0x000000A0
#define UART_MR_STOPMODE_2_BIT		0x00000080
#define UART_MR_STOPMODE_1_5_BIT	0x00000040
#define UART_MR_STOPMODE_1_BIT		0x00000000
#define UART_MR_PARITY_MASK		0x00000038
#define UART_MR_PARITY_NONE		0x00000020
#define UART_MR_PARITY_MARK		0x00000018
#define UART_MR_PARITY_SPACE		0x00000010
#define UART_MR_PARITY_ODD		0x00000008
#define UART_MR_PARITY_EVEN		0x00000000
#define UART_MR_CHARLEN_6_BIT		0x00000006
#define UART_MR_CHARLEN_7_BIT		0x00000004
#define UART_MR_CHARLEN_8_BIT		0x00000000
#define UART_MR_CHARLEN_MASK		0x00000006
#define UART_MR_CLKSEL			0x00000001
#define UART_MR_CHMODE_SHIFT	8
#define UART_MR_STOPMODE_SHIFT	6
#define UART_MR_PARITY_SHIFT	3
#define UART_MR_CHARLEN_SHIFT	1

/* IDR, IER, IMR, ISR */
#define UART_IXR_MASK		0x00001FFF
#define UART_IXR_TOVR		0x00001000
#define UART_IXR_TNFUL		0x00000800
#define UART_IXR_TTRIG		0x00000400
#define UART_IXR_DMS		0x00000200
#define UART_IXR_TOUT		0x00000100
#define UART_IXR_PARITY		0x00000080
#define UART_IXR_FRAMING	0x00000040
#define UART_IXR_OVER		0x00000020
#define UART_IXR_TXFULL		0x00000010
#define UART_IXR_TXEMPTY	0x00000008
#define UART_IXR_RXFULL		0x00000004
#define UART_IXR_RXEMPTY	0x00000002
#define UART_IXR_RXOVR		0x00000001

/* SR */
#define UART_SR_RXOVR	0x0001
#define UART_SR_RXEMPTY	0x0002
#define UART_SR_RXFULL	0x0004
#define UART_SR_TXEMPTY	0x0008
#define UART_SR_TXFULL	0x0010
#define UART_SR_RACTIVE	0x0400
#define UART_SR_TACTIVE	0x0800
#define UART_SR_FLOWDEL	0x1000
#define UART_SR_TTRIG	0x2000
#define UART_SR_TNFUL	0x4000

/* BAUDGEN */
#define UART_BAUDGEN_MASK	0x0000FFFF
#define UART_BAUDGEN_DISABLE	0x00000000
#define UART_BAUDGEN_RESET_VAL	0x0000028B

/* RXTOUT */
#define UART_RXTOUT_DISABLE 	0x00000000
#define UART_RXTOUT_MASK	0x000000FF

/* RXWM */
#define UART_RXWM_MASK		0x0000003F
#define UART_RXWM_RESET_VAL	0x00000020
#define UART_RXWM_DISABLE	0x00000000

/* MODEMCR */
#define UART_MODEMCR_FCM	0x00000010
#define UART_MODEMCR_RTS	0x00000002
#define UART_MODEMCR_DTR	0x00000001

/* MODEMSR */
#define UART_MODEMSR_FCMS	0x00000100
#define UART_MODEMSR_DCD	0x00000080
#define UART_MODEMSR_RI		0x00000040
#define UART_MODEMSR_DSR	0x00000020
#define UART_MODEMSR_CTS	0x00000010
#define UART_MODEMSR_DDCD	0x00000008
#define UART_MODEMSR_TERI	0x00000004
#define UART_MODEMSR_DDSR	0x00000002
#define UART_MODEMSR_DCTS	0x00000001

/* BAUDDIV */
#define UART_BAUDDIV_MASK	0x000000FF
#define UART_BAUDDIV_RESET_VAL	0x0000000F

/* FLOWDEL */
#define UART_FLOWDEL_MASK	0x0000003F

/* TXWM */
#define UART_TXWM_MASK		0x0000003F
#define UART_TXWM_RESET_VAL	0x00000020

void uart_init(void);
void uart_enable(void);
void uart_disable(void);
unsigned char uart_spin_getbyte(void);
void uart_spin_putbyte(unsigned char byte);

#endif
